var webpack = require('webpack');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var CopyWebpackPlugin = require('copy-webpack-plugin');
var TypedocWebpackPlugin = require('typedoc-webpack-plugin');
var commonsPlugin = new webpack.optimize.CommonsChunkPlugin('common.js');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var utils = require('./utils');

module.exports = function(options) {
    isProd = options.env === 'production';
    return {
        cache: true,

        resolve: {
            extensions: ['.js', '.json', '.ts']
        },

        module: {
            loaders: [{
                    test: /\.js$/,
                    loader: 'babel-loader',
                    exclude: /node_modules/,
                    //exclude: utils.root('src') + 'framework/data-provider/ref/*',
                    include: utils.root('src', 'assets'),
                    query: {
                        presets: ['es2015']
                    }
                },
                {
                    test: /\.ts$/,
                    // include: /src/,
                    exclude: /node_modules/,
                    loaders: [
                        //'@angularclass/hmr-loader?pretty=true&prod=true',// + !isProd + '&prod=' + isProd,
                        'awesome-typescript-loader',
                        'angular2-template-loader',
                        'angular2-router-loader'
                        //'angular2-router-loader?aot=true&genDir=src/compiled/src'
                    ],
                    exclude: [/\.(spec|e2e)\.ts$/]
                },
                {
                    test: /\.html$/,
                    loader: 'raw-loader'
                },

                {
                    test: /\.css$/,
                    include: utils.root('src'),
                    exclude: utils.root('src', 'assets'),
                    loader: 'raw' //raw-loader只针对.css文件
                },

                { test: /\.scss$/, exclude: utils.root('src', 'assets'), loaders: ['to-string-loader', 'css-loader', 'sass-loader'] },
                { test: /\.scss$/, include: utils.root('src', 'assets'), exclude: /\.useable\.scss$/, loader: ExtractTextPlugin.extract({ fallbackLoader: 'style-loader', loader: 'css!sass' }) },
                // { test: /\.useable\.scss$/, loader: "style/useable!css!sass" },

                { test: /\.(woff|woff2|ttf|eot|svg|map)$/, loader: 'file-loader?name=/assets/[name].[ext]' },
                { test: /\.(png|jpg|gif)$/, loader: 'url-loader?limit=8192&name=/assets/[name].[ext]' },
                { test: /\.json$/, include: utils.root('src'), loader: 'json-loader' }
            ]
        },

        plugins: [
            new webpack.BannerPlugin('Owned by WIT'),

            //commonsPlugin,

            // new webpack.optimize.CommonsChunkPlugin({ //common file
            //     name: ['ZZpolyfills', 'XXapp'].reverse()
            // }),

            // new HtmlWebpackPlugin({
            //     template: 'src/index.html'
            // }),

            // // new HtmlWebpackPlugin({
            // //     filename: 'notfound.html',
            // //     template: './src/notfound.html'
            // // }),

            // new CopyWebpackPlugin([
            //      {
            //         from: './src/lib/lib.js',
            //         to: 'js/lib.js',
            //         force: true
            //     },
            //     {
            //         from: './src/app.config.js',
            //         force: true
            //     },
            //     {
            //         from: './src/config',
            //         to: 'config',
            //         force: true
            //     }
            // ]),

            // new ExtractTextPlugin("assets/[name].css"),

            // new webpack.DllReferencePlugin({
            //     context: __dirname,
            //     manifest: require('../src/lib/manifest.json'),                
            // }),

            // new TypedocWebpackPlugin({
            //     name: 'WIT',
            //     mode: 'file',
            //     includeDeclarations: false,
            //     ignoreCompilerErrors: true,
            // })
        ]
    }
};

// function entries(globPath) {
//     var entries = {
//         'ZZpolyfills': './src/polyfills.ts',      
//         'XXapp': './src/main.ts',
//         'ZZstyle': './src/assets/lib.scss',
//         'boot': './src/assets/boot.scss',

//     };

//     //utils.getEntries(globPath, entries);
//     return entries;
// }